package com.hongyuan.sellcoach.ui.person.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.base.AppViewModelFactory;
import com.hongyuan.sellcoach.base.CustomActivity;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.databinding.ActivityCoachImgBinding;
import com.hongyuan.sellcoach.ui.person.beans.CoachImgBean;
import com.hongyuan.sellcoach.ui.person.view_mode.CoachImgViewModel;
import com.hongyuan.sellcoach.utils.GlideEngine;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.previewlibrary.GPreviewBuilder;
import com.previewlibrary.enitity.UserViewInfo;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class CoachImgActivity extends CustomActivity<ActivityCoachImgBinding, CoachImgViewModel> implements OnResultCallbackListener<LocalMedia> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_coach_img;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public CoachImgViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(CoachImgViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X3);
        setEnableLoadMore(Constants.GET_COACH_PHOTO_LIST);
        setEnableRefresh(Constants.GET_COACH_PHOTO_LIST);
        binding.comBack.setOnClickListener(view -> finish());

        binding.titleName.setText(getBundle().getString("title"));
        binding.showPhotoTitle.setText(getBundle().getString("title"));
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.clearParams().setParams("coach_mid",getBundle().getString("coach_mid"))
                .setParams("photo_category_id",getBundle().getString("photo_category_id"))
                .requestData(Constants.GET_COACH_PHOTO_LIST);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.goLookImg.observe(this,integer -> {

            //点击查看大图功能
            GPreviewBuilder.from(this)
                    .setData(viewModel.lookImgs.get())
                    .setCurrentIndex(integer)
                    .setType(GPreviewBuilder.IndicatorType.Dot)
                    .start();
        });

        viewModel.uc.goEdites.observe(this,flag -> {
            if(flag){
                //切换为编辑状态，不可刷新以及加载
                setOnRefresh(binding.refresh,REFRESH_0X4);

                binding.cancelEdits.setVisibility(View.VISIBLE);
                binding.bmageBox.setVisibility(View.GONE);
                binding.updateBox.setVisibility(View.GONE);
                binding.delectImgBox.setVisibility(View.VISIBLE);
            }else{
                //退出编辑状态，可刷新以及加载
                setOnRefresh(binding.refresh,REFRESH_0X3);

                binding.cancelEdits.setVisibility(View.GONE);
                binding.bmageBox.setVisibility(View.VISIBLE);
                binding.updateBox.setVisibility(View.VISIBLE);
                binding.delectImgBox.setVisibility(View.GONE);
            }
        });

        viewModel.uc.goUpdatePage.observe(this,flag -> {
            if(flag){
                //清楚上传照片的数据
                if(viewModel.mList != null){
                    viewModel.mList.clear();
                }
                //清除显示的数据
                viewModel.fileList.clear();
                //添加一个空对象
                viewModel.addNull();

                Animation anim = AnimationUtils.loadAnimation(this, R.anim.dialog_in_anim);
                binding.updateImgBox.startAnimation(anim);
                binding.updateImgBox.setVisibility(View.VISIBLE);
            }else{
                Animation anim = AnimationUtils.loadAnimation(this, R.anim.dialog_out_anim);
                binding.updateImgBox.startAnimation(anim);
                binding.updateImgBox.setVisibility(View.GONE);
            }
        });

        viewModel.uc.openFile.observe(this, new Observer<Void>() {
            @SuppressLint("CheckResult")
            @Override
            public void onChanged(Void aVoid) {

                //权限请求
                RxPermissions rxPermissions = new RxPermissions(CoachImgActivity.this);
                rxPermissions.request(Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS)
                        .subscribe(aBoolean -> {
                            PictureSelector.create(CoachImgActivity.this)
                                    .openGallery(PictureMimeType.ofImage())
                                    .loadImageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                                    .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
                                    .maxSelectNum(10-viewModel.fileList.size())// 最大图片选择数量 int
                                    .enableCrop(false)// 是否裁剪
                                    .compress(true)// 是否压缩
                                    .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                                    .previewVideo(true)//是否可预览视频
                                    .isZoomAnim(true)//图片列表点击 缩放效果 默认true
                                    .forResult(PictureConfig.CHOOSE_REQUEST,CoachImgActivity.this);
                        });
            }
        });
    }

    @Override
    public void onResult(List<LocalMedia> result) {
        viewModel.addShow(result);
    }

    @Override
    public void onCancel() {

    }
}
